package servlet.cookie;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class SetCookie1Servlet
 */
public class SetCookie1Servlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//쿠키 객체 생성(일시)해서 클라이언트에게 전송
		Date d = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dt = sdf.format(d); //쿠키에 넣을 Value
		Cookie c = new Cookie("dayTime", dt); //name : dateTime, value : dt
		c.setMaxAge(60*60*24*365); //1년간 유지해라
		
		//전송
		response.addCookie(c);
		
		//응답
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter out = response.getWriter();
		out.println("<html><body>");
		out.println("쿠키 값 설정한 일시 : "+ dt + "<br>");
		out.println("<a href='/myservlet/cookie/get_cookie.html'>쿠키 값 조회</a>");
		out.println("</body></html>");
	}

}
